const app = getApp();
const AJAX = app.$ajax,
  $wx = app.$wx;

Page({
  data: {
    showFiltrate: false,
    colorItem: -1,
    styleItem: -1,
    textureItem: -1,
    diydetailParams: {
      page: 1,
      name: '',
      color_id: 0,
      style_id: 0,
      info_id: 0
    }
  },

  onLoad(){
    this.getDiydetail();
  },

  filtrate(){
    this.setData({
      showFiltrate: true
    });
  },

  search(e){
    let td = this.data, serach_val = e.detail.val;

    td.diydetailParams.name = serach_val;
    this.getDiydetail();
  },

  filtrateMenuCancel(){
    this.setData({
      showFiltrate: false
    });
  },

  filtrateMenuConfirm(){
    let td = this.data, 
        diydetail = td.diydetail;

    td.diydetailParams.color_id = td.colorItem > - 1 ? diydetail.color_list[td.colorItem].id : 0,
    td.diydetailParams.style_id = td.styleItem > -1 ? diydetail.style_list[td.styleItem].id : 0,
    td.diydetailParams.info_id = td.textureItem > -1 ? diydetail.info_list[td.textureItem].id : 0;

    this.setData({
      showFiltrate: false
    }, callback => {
      this.getDiydetail();
    });
  },

  getColor(e) {
    let td = this.data, 
        ed = e.currentTarget.dataset;

    this.setData({
      colorItem: td.colorItem == ed.index ? -1 : ed.index
    });
  },

  getStyle(e) {
    let td = this.data, 
        ed = e.currentTarget.dataset;
    
    this.setData({
      styleItem: td.styleItem == ed.index ? -1 : ed.index
    });
  },

  getTexture(e){
    let td = this.data, 
        ed = e.currentTarget.dataset;

    this.setData({
      textureItem: td.textureItem == ed.index ? -1 : ed.index
    });
  },

  selectedTxture(e){
    let td = this.data, 
        ed = e.currentTarget.dataset,
        diydetail = td.diydetail;

    console.log(td);
    app.appData.selected_txture_item = ed.item;
    app.appData.selected_txture_itemParams = {
      color_id: td.diydetailParams.color_id,
      caizhi_id: td.diydetailParams.info_id,
      fengge_id: td.diydetailParams.style_id
    };

    wx.navigateBack();
  },

  getDiydetail(){
    let td = this.data;
    AJAX.diydetail({
      loadType: 'bar',
      ...td.diydetailParams
    }).then(([status, res, msg]) => {
      console.log(res);
      if(status){
        this.setData({
          diydetail: res
        });
      }
    });
  }
});